package com.mashang.service;

import com.mashang.common.PageVo;
import com.mashang.domain.Clazz;
import com.mashang.domain.Student;
import com.mashang.mapper.ClazzMapper;
import com.mashang.mapper.StudentMapper;
import com.mashang.vo.student.StudentPageView;
import com.mashang.vo.student.StudentQueryVo;
import com.mashang.vo.student.StudentView;

import java.util.ArrayList;
import java.util.List;

public class StudentService {

    public StudentPageView selectList(StudentQueryVo vo) {

        //1.查询学生列表
        StudentMapper studentMapper = new StudentMapper();
        Student queryStu = new Student();
        queryStu.setName(vo.getName());
        queryStu.setStudentNo(vo.getStudentNo());

        List<Student> studentList = studentMapper.selectStudentPageList(queryStu, vo.getPageVo());

        List<StudentView> studentViewList = new ArrayList<>();
        ClazzMapper clazzMapper = new ClazzMapper();
        for (Student student : studentList) {
            StudentView studentView = new StudentView();
            studentView.modelTranfView(student);
            // 根据学生所属的班级id查询对应的班级名称
            Clazz clazz = clazzMapper.selectById(student.getClassId());
            studentView.setClassName(clazz == null ? null : clazz.getName());
            studentViewList.add(studentView);
        }

        StudentPageView studentPageView = new StudentPageView();
        studentPageView.setStudentViewList(studentViewList);

        //获取总数
        studentPageView.setTotal(studentMapper.count(queryStu, vo.getPageVo()));

        return studentPageView;
    }


}
